package com.xci.sys.api;

import com.xci.core.annotation.Authorize;
import com.xci.core.domain.BoolMessage;
import com.xci.core.internal.Const;
import com.xci.core.base.PrimaryKeyValue;
import com.xci.core.base.RestMessage;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.base.ApiController;
import com.xci.sys.entity.SysDic;
import com.xci.sys.entity.SysDicCategory;
import com.xci.sys.filter.DicFilter;
import com.xci.sys.service.DicCategoryService;
import com.xci.sys.service.DicService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典接口
 *
 * @author 吕艳阳
 */
@ApiSort(5)
@Api(tags = "字典接口")
@Authorize
@RestController
@RequestMapping(value = "/api/sys/dic", produces = Const.P_JSON)
public class DicApiController extends ApiController {
    @Resource
    private DicCategoryService dicCategoryService;//字典类型服务
    @Resource
    private DicService dicService;//字典服务

    /*--------------字典类型接口--------------*/

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "检查字典类型编码是否存在")
    @ApiImplicitParam(name = "code", value = "字典类型编码", required = true)
    @PostMapping("/existDicCategoryByCode/{code}")
    public RestMessage existDicCategoryByCode(@PathVariable String code) {
        return RestMessage.result(dicCategoryService.existByCode(code));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "新建字典类型")
    @Authorize(code = "sys.dic.insertDicCategory")
    @PostMapping("/insertDicCategory")
    public RestMessage insertDicCategory(@RequestBody SysDicCategory entity) {
        return dicCategoryService.insert(entity);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "修改字典类型")
    @Authorize(code = "sys.dic.updateDicCategory")
    @PostMapping("/updateDicCategory")
    public RestMessage updateDicCategory(@RequestBody SysDicCategory entity) {
        return dicCategoryService.update(entity);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量修改字段值")
    @PostMapping("/batchUpdate")
    public RestMessage batchUpdateDicCategory(@RequestBody List<PrimaryKeyValue> keyValues) {
        return dicCategoryService.batchUpdate(keyValues);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除字典类型")
    @ApiImplicitParam(name = "ids", value = "字典类型主键字符串,多个逗号隔开", required = true)
    @Authorize(code = "sys.dic.deleteDicCategory")
    @PostMapping("/deleteDicCategory")
    public RestMessage deleteDicCategory(String ids) {
        return dicCategoryService.delete(ids);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "查询单个字典类型")
    @ApiImplicitParam(name = "id", value = "字典类型主键", required = true)
    @PostMapping("/selectDicCategoryById/{id}")
    public RestMessage<SysDicCategory> selectDicCategoryById(@PathVariable String id) {
        return RestMessage.success(dicCategoryService.selectById(id));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "根据编码查询单个字典类型")
    @ApiImplicitParam(name = "code", value = "字典类型编码", required = true)
    @PostMapping("/selectDicCategoryByCode/{code}")
    public RestMessage<SysDicCategory> selectDicCategoryByCode(@PathVariable String code) {
        return RestMessage.success(dicCategoryService.selectByCode(code));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "查询字典类型列表")
    @PostMapping("/selectListDicCategory")
    public RestMessage<List<SysDicCategory>> selectListDicCategory() {
        return RestMessage.success(dicCategoryService.selectList());
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "导出字典类型列表")
    @PostMapping(value = "/exportDicCategory", produces = {Const.P_OCTET, Const.P_JSON})
    public void exportDicCategory() {
        ExcelHelper.exportWeb(dicCategoryService.selectList(), SysDicCategory.class, "系统字典类型列表");
    }

    /*----------字典接口---------------------------------------------------------------------------------------------------------*/

    @ApiOperationSupport(order = 50)
    @ApiOperation(value = "新建字典")
    @Authorize(code = "sys.dic.insert")
    @PostMapping("/insert")
    public RestMessage insert(@RequestBody SysDic entity) {
        return dicService.insert(entity);
    }

    @ApiOperationSupport(order = 51)
    @ApiOperation(value = "修改字典")
    @Authorize(code = "sys.dic.update")
    @PostMapping("/update")
    public RestMessage update(@RequestBody SysDic entity) {
        return dicService.update(entity);
    }

    @ApiOperationSupport(order = 52)
    @ApiOperation(value = "修改字典状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "字典主键字符串", required = true),
            @ApiImplicitParam(name = "status", value = "字典状态", required = true)
    })
    @Authorize(code = "sys.dic.updateStatus")
    @PostMapping("/updateStatus")
    public RestMessage updateStatus(String ids, Boolean status) {
        return dicService.updateStatus(ids, status);
    }

    @ApiOperationSupport(order = 53)
    @ApiOperation(value = "删除字典")
    @ApiImplicitParam(name = "ids", value = "字典主键字符串,多个逗号隔开", required = true)
    @Authorize(code = "sys.dic.delete")
    @PostMapping("/delete")
    public RestMessage delete(String ids) {
        return dicService.delete(ids);
    }

    @ApiOperationSupport(order = 54)
    @ApiOperation(value = "根据字典类型编码删除")
    @ApiImplicitParam(name = "code", value = "字典类型编码", required = true)
    @Authorize(code = "sys.dic.delete")
    @PostMapping("/deleteByCode/{code}")
    public RestMessage deleteByCode(@PathVariable String code) {
        return dicService.deleteByCode(code);
    }

    @ApiOperationSupport(order = 55)
    @ApiOperation(value = "检查字典编码是否存在")
    @ApiImplicitParam(name = "code", value = "字典编码", required = true)
    @PostMapping("/existByCode/{code}")
    public RestMessage<Boolean> existByCode(@PathVariable String code) {
        return RestMessage.result(dicService.existByCode(code));
    }

    @ApiOperationSupport(order = 56)
    @ApiOperation(value = "查询单个字典")
    @ApiImplicitParam(name = "id", value = "字典主键", required = true)
    @PostMapping("/selectById/{id}")
    public RestMessage<SysDic> selectById(@PathVariable String id) {
        return RestMessage.success(dicService.selectById(id));
    }

    @ApiOperationSupport(order = 57)
    @ApiOperation(value = "根据类型编码查询字典数量")
    @ApiImplicitParam(name = "code", value = "字典类型编码", required = true)
    @PostMapping("/selectCountByCode/{code}")
    public BoolMessage<Long> selectCountByCode(@PathVariable String code) {
        return BoolMessage.success(dicService.selectCountByCode(code));
    }

    @ApiOperationSupport(order = 57)
    @ApiOperation(value = "根据类型编码查询字典列表")
    @ApiImplicitParam(name = "code", value = "字典类型编码", required = true)
    @PostMapping("/selectListByCode/{code}")
    public RestMessage<List<SysDic>> selectListByCode(@PathVariable String code) {
        return RestMessage.success(dicService.selectListByCode(code));
    }

    @ApiOperationSupport(order = 58)
    @ApiOperation(value = "查询字典列表")
    @PostMapping("/selectList")
    public RestMessage<List<SysDic>> selectList(DicFilter filter) {
        return RestMessage.success(dicService.selectList(filter));
    }

    @ApiOperationSupport(order = 59)
    @ApiOperation(value = "导出字典列表")
    @Authorize(code = "sys.dic.export")
    @PostMapping(value = "/export", produces = {Const.P_OCTET, Const.P_JSON})
    public void export(DicFilter filter) {
        ExcelHelper.exportWeb(dicService.selectList(filter), SysDic.class, "系统字典列表");
    }

    @ApiOperationSupport(order = 60)
    @ApiOperation(value = "刷新字典缓存")
    @Authorize(code = "sys.dic.refreshCache")
    @PostMapping(value = "/refreshCache")
    public RestMessage refreshCache() {
        dicService.refreshCache();
        return RestMessage.success();
    }
}